﻿package net.wangit.framework.session;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.Visitor;
import net.wangit.context.Message;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.util.UnitUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


@SuppressWarnings("serial")
public class AgileSession implements Serializable {
	private static final Log log = LogFactory.getLog(AgileSession.class);
	public static final String KEY = "net.wangit.framework.session.AgileSession";

	List<String> accessChain; // 访问地址链
	private Visitor visitor;
	private Map uss;
	Map invalidFields;
	private Map<String, String> tokens;
	private String records;
	private Map snapshots;
	private Message message;
	private String actionScript;

	public AgileSession() {
		accessChain = new ArrayList<String>();
		uss = new HashMap();
		tokens = new HashMap<String, String>();
		visitor = new Visitor();
	}

	public void reset() {
		if (invalidFields != null) {
			invalidFields.clear();
			invalidFields = null;
		}
		if (snapshots != null) {
			snapshots.clear();
			snapshots = null;
		}
		records = null;
		message = null;
		actionScript = null;
	}

	public void clearAccessChain() {
		accessChain.clear();
	}

	public void addAccessChain(String url) {
		if (Checker.isEmpty(url))
			return;
		if (accessChain.size() == 0) {
			accessChain.add(url);
			return;
		}
		String lastUrl = accessChain.get(accessChain.size() - 1);
		if (!lastUrl.equals(url))
			accessChain.add(url);
		if (log.isDebugEnabled())
			log.debug((new StringBuilder("Add access chain: ")).append(url)
					.toString());
	}

	public String getCurrentUrl() {
		if (!Checker.isEmpty(accessChain))
			return accessChain.get(accessChain.size() - 1);
		else
			return null;
	}

	public String gePreviousUrl() {
		if (accessChain != null && accessChain.size() > 0) {
			accessChain.remove(accessChain.size() - 1);
		}
		return getCurrentUrl();
	}

	public UnitSettings getUnitSettings(String key) {
		UnitSettings us = (UnitSettings) uss.get(key);
		if (us == null) {
			us = new UnitSettings();
			uss.put(key, us);
		}
		return us;
	}

	public UnitSettings getUnitSettings(String domain, String unitId) {
		return getUnitSettings(UnitUtil.getKey(domain, unitId));
	}

	public Visitor getVisitor() {
		return visitor;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public MapList getSnapshot(UnitComponent uc) {
		if (snapshots != null)
			return (MapList) snapshots.get(UnitUtil.getKey(uc));
		else
			return null;
	}

	public void setSnapshots(Map snapshots) {
		this.snapshots = snapshots;
	}

	public String getActionRecords() {
		return records;
	}

	public void setActionRecords(String records) {
		this.records = records;
	}

	public String getToken(String key) {
		return tokens.get(key);
	}

	public void setToken(String key, String token) {
		tokens.put(key, token);
	}

	public Map getInvalidFields() {
		return invalidFields;
	}

	public void setInvalidFields(Map invalidFields) {
		this.invalidFields = invalidFields;
	}

	public String getActionScript() {
		return actionScript;
	}

	public void setActionScript(String actionScript) {
		this.actionScript = actionScript;
	}

}
